﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace PPCodes.Log
{
    /// <summary>
    /// Log4net配置设置类
    /// </summary>
    public class Log4netConfig
    {
        private static readonly object locker = new object();

        /// <summary>
        /// 初始化log4net配置
        /// </summary>
        /// <param name="configFile">FileInfo</param>
        public static void SetConfig(FileInfo configFile, bool isWatchFile)
        {
            //为了保证只初始化一次，需要考虑线程安全
            if (!log4net.LogManager.GetRepository().Configured)
            {
                lock (locker)
                {
                    if (!log4net.LogManager.GetRepository().Configured)
                    {
                        if (isWatchFile)
                        {
                            log4net.Config.XmlConfigurator.ConfigureAndWatch(configFile);
                        }
                        else
                        {
                            log4net.Config.XmlConfigurator.Configure(configFile);
                        }
                    }
                }
            }
        }
    }
}
